﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using MockCommon;
using MockDataTransferObject;
using System.Collections.ObjectModel;

namespace MockDataAccess
{
    public class UserDataAccess : BaseDataAccess<User>
    {
        private Collection<SqlParameter> _parameters;
        
        /// <summary>
        /// Initializes a new instance of the <see cref="UserDataAccess"/> class.
        /// </summary>
        public UserDataAccess()
            : base()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UserDataAccess"/> class.
        /// </summary>
        /// <param name="isFake">if set to <c>true</c> [is fake].</param>
        public UserDataAccess(bool isFake)
            : base(isFake)
        {
        }

        #region Is
        /// <summary>
        /// Determines whether [is exist user name] [the specified user name].
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <returns>
        ///   <c>true</c> if [is exist user name] [the specified user name]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsExistUserName(string userName)
        {
            _parameters = new Collection<SqlParameter>();
            _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterUserName, Value = userName });
            object result = ExecuteQueryAsScalar(Constant.UserExistUserName, _parameters);
            if (result == null)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// Determines whether [is exist user name and is active] [the specified user name].
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        /// <returns>
        ///   <c>true</c> if [is exist user name and is active] [the specified user name]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsExistUserNameAndIsActive(string userName, bool isActive)
        {
            _parameters = new Collection<SqlParameter>();
            _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterUserName, Value = userName });
            _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterIsActive, Value = isActive });
            object result = ExecuteQueryAsScalar(Constant.UserExistUserNameAndIsActive, _parameters);
            if (result != null)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region Get
        /// <summary>
        /// Gets the name of the user id by user.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <returns></returns>
        public object GetUserIdByUserName(string userName)
        {
            _parameters = new Collection<SqlParameter>();
            _parameters.Add(new SqlParameter(Constant.ParameterUserName, SqlDbType.VarChar) { Value = userName });
            return ExecuteQueryAsScalar(Constant.UserGetUserIdByUserName, _parameters);
        }

        /// <summary>
        /// Gets the by user name and password.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public User GetByUserNameAndPassword(string userName, string password)
        {
            try
            {
                _parameters = new Collection<SqlParameter>();
                _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterUserName, Value = userName });
                _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterPassword, Value = password });
                DataTable dataTable = ExecuteQueryAsDataTable(Constant.UserAuthentication, _parameters);
                Collection<AbstractEntity> users = ConvertToList(dataTable, typeof(User));
                if (users != null && users.Count > 0)
                {
                    return (User)users[0];
                }
            }
            catch (InvalidCastException exception)
            {
                throw new MockException(exception.Message, "DataAccess.GetByUsernameAndPassword");
            }
            return null;
        }

        /// <summary>
        /// Gets the name of the by user.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <returns></returns>
        public User GetByUserName(string userName)
        {
            try
            {
                _parameters = new Collection<SqlParameter>();
                _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterUserName, Value = userName });

                DataTable dataTable = ExecuteQueryAsDataTable(Constant.UserGetByUserName, _parameters);
                Collection<AbstractEntity> users = ConvertToList(dataTable, typeof(User));
                if (users != null && users.Count > 0)
                {
                    return (User)users[0];
                }
            }
            catch (InvalidCastException exception)
            {
                throw new MockException(exception.Message, "DataAccess.GetByUsername");
            }
            return null;
        }
        #endregion
    }
}
